﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using log4net;

namespace TDLib.Tools
{
    /// <summary>
    /// log4net的封装
    /// </summary>
    public class Log
    {
        private static bool IsInit = false;
        private static readonly string strLogFileName = "Log";

        private static void InitLog(string logFileName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
            sb.Append("<configuration>r\n");
            sb.Append("\t<configSections>r\n");
            sb.Append("\t\t<section name=\"log4net\" type=\"log4net.Config.Log4NetConfigurationSectionHandler, log4net\" />\r\n");
            sb.Append("\t</configSections>r\n");
            sb.Append("\t<log4net debug=\"false\">\r\n");
            sb.Append("\t\t<root>\r\n");
            sb.Append("\t\t\t<level value=\"ALL\" />\r\n");
            sb.Append("\t\t\t<appender-ref ref=\"rollingFile\" />\r\n");
            sb.Append("\t\t</root>\r\n");
            sb.Append("<appender  name=\"rollingFile\" type=\"log4net.Appender.RollingFileAppender,log4net\" >\r\n");
            sb.Append("<param name=\"File\" value=\"log\\" + logFileName + "\" />\r\n");
            sb.Append("<param name=\"AppendToFile\" value=\"true\" />\r\n");
            sb.Append("<param name=\"RollingStyle\" value=\"Date\" />\r\n");
            sb.Append("<param name=\"DatePattern\" value=\"yyyy-MM-dd&quot;.txt&quot;\"/>\r\n");
            sb.Append("<param name=\"StaticLogFileName\" value=\"false\" />\r\n");
            sb.Append("<param name=\"MaxSizeRollBackups\" value=\"100\" />\r\n");
            sb.Append("<param name=\"MaximumFileSize\" value=\"5MB\" />\r\n");
            sb.Append("<layout type=\"log4net.Layout.PatternLayout,log4net\">\r\n");
            sb.Append("<param name=\"ConversionPattern\" value=\"%d [%t] %-5p %c - %m%n\" />\r\n");
            sb.Append("<param name=\"Header\" value=\"&#13;&#10;----------------------header--------------------------&#13;&#10;\" />\r\n");
            sb.Append("<param name=\"Footer\" value=\"&#13;&#10;----------------------footer--------------------------&#13;&#10;\" />\r\n");
            sb.Append("</layout>\r\n");
            sb.Append("</appender>\r\n");
            sb.Append("<appender name=\"consoleApp\" type=\"log4net.Appender.ConsoleAppender,log4net\">\r\n");
            sb.Append("<layout type=\"log4net.Layout.PatternLayout,log4net\">\r\n");
            sb.Append("<param name=\"ConversionPattern\" value=\"%d [%t] %-5p %c - %m%n\" />\r\n");
            sb.Append("</layout>\r\n");
            sb.Append("</appender>\r\n");
            sb.Append("<logger name=\"Log4NetTest.LogTest\">\r\n");
            sb.Append("<level value=\"ALL\" />\r\n");
            sb.Append("<appender-ref  ref=\"rollingFile\" />\r\n");
            sb.Append("</logger>\r\n");
            sb.Append("</log4net>\r\n");
            sb.Append("</configuration>\r\n");

            MemoryStream mem = new MemoryStream(System.Text.UTF8Encoding.Default.GetBytes(sb.ToString()));
            log4net.Config.DOMConfigurator.Configure(mem);            
        }




        /// <summary>
        /// 输出Debug信息
        /// </summary>
        /// <param name="msg"></param>
        public static void WriteDebug(string msg)
        {
            if (!IsInit)
            {
                InitLog(strLogFileName);
                IsInit = true;
            }

            LogManager.GetLogger(strLogFileName).Debug(msg);
        }

        /// <summary>
        /// 输出正常信息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteInfo(string info)
        {
            if (!IsInit)
            {
                InitLog(strLogFileName);
                IsInit = true;
            }

            ILog log = LogManager.GetLogger(strLogFileName);

            log.Info(info);
        }

        /// <summary>
        /// 输出致命错误信息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteFatal(string info)
        {
            if (!IsInit)
            {
                InitLog(strLogFileName);
                IsInit = true;
            }


            LogManager.GetLogger(strLogFileName).Fatal(info); 
        }

        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteErr(string info)
        {
            if (!IsInit)
            {
                InitLog(strLogFileName);
                IsInit = true;
            }

            ILog log = LogManager.GetLogger(strLogFileName);

            log.Error(info);
        }

        /// <summary>
        /// 输出警告信息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteWarn(string info)
        {
            if (!IsInit)
            {
                InitLog(strLogFileName);
                IsInit = true;
            }

            ILog log = LogManager.GetLogger(strLogFileName);

            log.Warn(info);
        }
    }
}
